//
//  HDSaveImage.swift
//  HuaDaoPeifu
//
//  Created by 刘汉琳 on 16/5/13.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit
// FIXME: comparison operators with optionals were removed from the Swift Standard Libary.
// Consider refactoring the code to use the non-optional operators.
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l < r
  case (nil, _?):
    return true
  default:
    return false
  }
}

// FIXME: comparison operators with optionals were removed from the Swift Standard Libary.
// Consider refactoring the code to use the non-optional operators.
fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l > r
  default:
    return rhs < lhs
  }
}


struct HDSaveImagePath{
   static var kDocumentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]

}

class HDSaveImage: NSObject {
    
    class func saveImageWithImageName(_ image:UIImage,caseId:String,dataType:String,imageCount:Int){
        
        let path:String = AppConstant.documentsPath + "/hdsaveImage/"
        let imagePath = path + caseId + "/" + dataType + "/" + String(imageCount)

        do {
            try FileManager.default.createDirectory(atPath: imagePath, withIntermediateDirectories: true, attributes: nil)
        } catch _ {}
        
        let  status = (try? UIImageJPEGRepresentation(image, 0.5)?.write(to: URL(fileURLWithPath: imagePath + ".png"), options: [.atomic])) != nil
        print(status)
        
    }
    
    class func imageWithMessage(_ caseId:String,dataType:String,imageCount:Int) ->AnyObject?{
    
        let path:String = AppConstant.documentsPath + "/hdsaveImage/"
        
        let imagePath = path + caseId + "/" + dataType + "/" + String(imageCount) + ".png"
        let filePath = FileManager.default
        if filePath.fileExists(atPath: imagePath) {
            return UIImage(contentsOfFile:imagePath)!

        }else{
            return nil
        }
    }
    
    
    class func savePictureWith(_ image:[HDPayoutSaveImageModel],caseId:String,dataType:String){
        
        
       let data =  NSKeyedArchiver.archivedData(withRootObject: image)
        
        let path:String = AppConstant.documentsPath + ("/" + caseId + dataType + ".plist")

        try? data.write(to: URL(fileURLWithPath: path), options: [.atomic])
        if (try? data.write(to: URL(fileURLWithPath: path), options: [.atomic])) != nil {
            print("saveTrue")
        }

    }
    
    
    class func getPictureWith(_ caseId:String,dataType:String)->AnyObject? {
        let fileManager = FileManager.default
        let path:String = AppConstant.documentsPath + ("/" + caseId + dataType + ".plist")
        if fileManager.fileExists(atPath: path) {
            print("hao")
            let data = try? Data(contentsOf: URL(fileURLWithPath: path))
            let arry = NSKeyedUnarchiver.unarchiveObject(with: data!)
            if (arry as AnyObject).count>0 {
                return arry as AnyObject
            }else{
                removeSaveFile(caseId, dataType: dataType)
                return nil
            }
            
        }else{
            print("false")
            return nil
        }
    }
    
    
    class func removeSaveFile(_ caseId:String,dataType:String){
        let fileManager = FileManager.default
        let path:String = AppConstant.documentsPath + ("/" + caseId + dataType + ".plist")
        if fileManager.fileExists(atPath: path) {
            do{
                try fileManager.removeItem(atPath: path)

            }catch{}
        }
    }
    
    

}
